package com.qidianit.springboot.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.qidianit.springboot.entity.Student;
import com.qidianit.springboot.util.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 * @date 2025/5/6 下午 1:49
 * @description
 **/

@RestController
@CrossOrigin
public class RedisController {
    @Resource
    Jedis jedis;

    @Autowired
    ValueOperations<String, String> valueOperations;

    @Autowired
    RedisUtil redisUtil;

    @RequestMapping("/setString")
    public String setString() {
       // System.out.println(jedis.set("good", "morning"));

        valueOperations.set("A","65");
        return "success";
    }

    @RequestMapping("/save")
    public String save() {
        Student student=new Student();
        student.setAge(18);
        student.setId(1001);
        student.setStu_name("张三");
        jedis.set("student", JSON.toJSONString(student));
        return  "success";
    }



    @RequestMapping("/getObj")
    public Student getObj() {
      Student student=  JSONObject.parseObject(jedis.get("student"),Student.class);
        return student;
    }



    @RequestMapping("/saveObj")
    public void saveObj() {
        Student student = new Student();
        student.setAge(18);
        student.setId(1001);
        student.setStu_name("张三");
        redisUtil.set("stu",student);

        redisUtil.expire("stu",10);
//      Student stu=  redisUtil.get("stu",Student.class);
//      System.out.println(stu);
    }


    @RequestMapping("/saveList")
    public void saveList() {
        Student student = new Student(1001,"张三",18);
        Student student2 = new Student(1002,"张三2",18);
        Student student3 = new Student(1003,"张三3",18);
        List<Student> list = new ArrayList<>();

        student2.setAge(20);
        list.add(student);
        list.add(student2);
        list.add(student3);

        redisUtil.set("myList",list);
      List<Student> myList=  redisUtil.getList("myList",Student.class);
        System.out.println(myList);
       // redisUtil.expire("stu",10);
// Student stu=  redisUtil.get("stu",Student.class);
// System.out.println(stu);
    }

    @RequestMapping("/login")
    public String login(String username) {
        System.out.println(username);
        if(true){//登录成功
          redisUtil.set(username,username);
          redisUtil.expire(username,20);
        }
        return "ok";
    }

    @RequestMapping("/check")
    public String check(String username) {
        if(Objects.nonNull(username)){
            if(Objects.isNull(redisUtil.get(username))){
                return "登录已过期";
            }
        }else{
            return "未登录";
        }
        return "ok";
    }

    @RequestMapping("/createOrderNum")
    public String createOrderNum() {
        return redisUtil.generateOrderNo();
    }


}
